'use strict';
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
module.exports = (app) => {
  class AdminService extends app.Service {
    //获取项目所有博客
    async getProjectBlog() {
      const { app, ctx } = this;
      const { Blog } = app.model.Tables.Blog;
      let res = await Blog.findAndCountAll({
        where: {
          projectId: ctx.adminInfo.project_id,
        },
        limit: Number(data.limit) || 10,
        offset: Number(data.offset) || 0,
      });
      return res;
    }
    //管理员根据关键词搜索博客
    async getByKeyword() {
      const { app, ctx } = this;
      const { Blog } = app.model.Tables.Blog;
      const data = ctx.query;
      /** 根据标题模糊查询 */
      let titleRes = await Blog.findAll({
        where: {
          title: { [Op.like]: '%' + data.keyword + '%' },
          projectId: ctx.adminInfo.project_id,
        },
      });
      /** 根据内容模糊查询 */
      let contentRes = await Blog.findAll({
        where: {
          content: { [Op.like]: '%' + data.keyword + '%' },
          projectId: ctx.adminInfo.project_id,
        },
      });
      let allRes = titleRes.concat(contentRes);
      //去重
      let res = allRes.reduce((prev, cur) => {
        let id = prev.map((item) => {
          return item.id;
        });
        return id.includes(cur.id) ? prev : [...prev, cur];
      }, []);
      return res;
    }
    //删除博客
    async deleteBlog() {
      const { app, ctx } = this;
      const { Blog, User, BlogClassify } = app.model.Tables.Blog;
      const data = ctx.request.body;

      //查找作者
      let userRes = await Blog.findOne({
        where: {
          id: data.blogId,
        },
      });
      //删除
      let res = await Blog.destroy({
        where: {
          id: data.blogId,
          projectId: ctx.adminInfo.project_id,
        },
      });
      //删除博客分类信息
      await BlogClassify.destroy({
        where: {
          blogId: data.blogId,
          projectId: ctx.adminInfo.project_id,
        },
      });
      //作者博客数自减1
      await User.findByPk(userRes.dataValues.userId).then(function (user) {
        user.decrement('blogCount').then(function (user) {
          console.log('success');
        });
      });
      return res;
    }
    //获取博客所有评论
    async getBlogComment() {
      const { app, ctx } = this;
      const { Comment } = app.model.Tables.Blog;
      const data = ctx.query;

      let res = await Comment.findAndCountAll({
        where: {
          blogId: data.blogId,
          projectId: ctx.adminInfo.project_id,
        },
        limit: Number(data.limit) || 10,
        offset: Number(data.offset) || 0,
      });
      return res;
    }

    //删除评论
    async deleteComment() {
      const { app, ctx } = this;
      const { Comment, User, Blog } = app.model.Tables.Blog;
      const commentId = ctx.params.id;

      //根据评论id查找用户
      let uRes = await Comment.findOne({
        where: {
          id: commentId,
        },
      });

      //用户评论数自减1
      await User.findByPk(uRes.dataValues.userId).then(function (user) {
        user.decrement('commentCount').then(function (user) {
          console.log('success');
        });
      });
      //博客评论数自减1
      await Blog.findByPk(uRes.dataValues.blogId).then(function (blog) {
        blog.decrement('comment_count').then(function (blog) {
          console.log('success');
        });
      });
      //删除评论记录
      let res = await Comment.destroy({
        where: {
          id: commentId,
          projectId: ctx.adminInfo.project_id,
        },
      });
      return res;
    }

    async getProjectComment() {
      const { app, ctx } = this;
      const { Comment } = app.model.Tables.Blog;
      const data = ctx.query;

      let res = await Comment.findAndCountAll({
        where: {
          projectId: ctx.adminInfo.project_id,
        },
        limit: Number(data.limit) || 10,
        offset: Number(data.offset) || 0,
      });
      return res;
    }

    async getBlogById() {
      const { app, ctx } = this;
      const { Blog, BlogClassify, LikeBlog } = app.model.Tables.Blog;
      const data = ctx.query;
      let res = await Blog.findOne({
        where: {
          id: data.id,
          projectId: ctx.adminInfo.project_id,
        },
        //连表查找该博客分类 点赞记录 作者信息
        include: [
          {
            model: BlogClassify,
            //字段
            attributes: ['classifyId', 'classifyName'],
          },
          {
            model: LikeBlog,
            attributes: ['likeUserId'],
          },
        ],
      });

      return res;
    }
  }
  return AdminService;
};
